Use sets to keep unique and Binary Form 1111 to the possible results. The code is :
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
class Solution {
public:
set<double> status[16];
typedef set<double>::iterator ITER;
void f(int x, vector<int>& vec) {
for (int i = 1; i <= x / 2; ++i) {
if ((x & i) == i) {
for (ITER j = status[i].begin(); j != status[i].end(); ++j)
for (ITER k = status[x-i].begin(); k != status[x-i].end(); ++k) {
status[x].insert((*j) * (*k));
status[x].insert((*j) + (*k));
status[x].insert((*j) - (*k));
status[x].insert((*k) - (*j));
if ((*k) != 0)
status[x].insert((*j) / (*k));
if ((*j) != 0)
status[x].insert((*k) / (*j));
}
}
}
}
void init(const vector<int>& vec) {
for (int i = 0; i < vec.size(); ++i)
status[(1 << i)].insert((double)vec[i]);
}
bool getSum24(vector<int>& vec) {
init(vec);
for (int i = 1; i <= 15; ++i)
f(i, vec);
return status[15].find(24) != status[15].end() ? true : false;
}
};
int main() {
int a[] = {1, 1, 1, 1};
Solution s;
vector<int> vec(a, a+4);
bool res = s.getSum24(vec);
return 0;
}
import math
class Test:
def Sum24(self, nums):
self.status = {i: set() for i in range(1, 16)}
for i in range(4):
self.status[1<<i].add(nums[i])
for groupindex in range(1,16):
for i in range(1, math.ceil((groupindex+1)/2)):
if (i & groupindex == i):
subgroupindex = i
relativegroupindex = groupindex -subgroupindex
for k in self.status[subgroupindex]:
for l in self.status[relativegroupindex]:
self.status[groupindex].add(k + l)
self.status[groupindex].add(k - l)
self.status[groupindex].add(l - k)
self.status[groupindex].add(k * l)
if (k != 0):
self.status[groupindex].add(l / k)
if (l != 0):
self.status[groupindex].add(k / l)
return self.status
test = Test()
res = test.Sum24([1,1,1,1])
print(24 in res[15])
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *
, /
, +
, -
, (
, )
to get the value of 24.
Example 1:
Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24
Example 2:
Input: [1, 2, 1, 2] Output: False
Note:
- The division operator
/
represents real division, not integer division. For example, 4 / (1 - 2/3) = 12. - Every operation done is between two numbers. In particular, we cannot use
-
as a unary operator. For example, with[1, 1, 1, 1]
as input, the expression-1 - 1 - 1 - 1
is not allowed. - You cannot concatenate numbers together. For example, if the input is
[1, 2, 1, 2]
, we cannot write this as 12 + 12.
--------------------------------------------------------------------
class Solution:
def judgePoint24(self, nums) -> bool:
l = len(nums)
if (l != 4):
return False
set24 = [set() for i in range(16)]
for i in range(4):
idx = (1<<i)
set24[idx].add(nums[i])
for i in range(2,16):
if not set24[i]:
for j in range(1, (i>>1)+1):
if (j & i == j):
for a in set24[j]:
for b in set24[i-j]:
set24[i].add(a*b)
set24[i].add(a+b)
set24[i].add(a-b)
set24[i].add(b-a)
if (a != 0):
set24[i].add(b/a)
if (b != 0):
set24[i].add(a/b)
for val in set24[15]:
if abs(val-24) < 0.1:
return True
return False
s = Solution()
print(s.judgePoint24([1, 2, 1, 2]))